<template>
<div>
  <h1>登录页面</h1>
  <el-form>
    <el-form-item label="用户名">
      <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="user.password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="login()">登录</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      user: {username: "",password:""}
    }
  },
  methods: {
    login() {
      // username=tom&password=123
      let data = this.qs.stringify(this.user);
      this.axios.post("http://localhost:8080/v1/users/login",data)
      .then((response)=>{
          if (response.data.code==1){
            console.log(response.data.data.nickname);
            this.$message.success("登录成功!");
            //把服务器响应的用户信息保存到LocalStorage里面供其他页面使用
            //JSON.stringify(对象)  将对象转成Json格式的字符串
            //response.data=JsonResult对象(code,msg,data)
            let jsonStr = JSON.stringify(response.data.data);
            localStorage.setItem('user',jsonStr);

            this.$router.push('/');
          }else{
            this.$message.error(response.data.msg);
          }
      })
    }
  },
}
</script>

<style scoped>

</style>